package gameUtils;

import games.Map;
import games.RankingEntry;
import games.gameObjects.Grass;
import games.gameObjects.Path;
import games.gameObjects.Wall;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

/**
 * 
 * @author batata
 * Class that reads and writes to files
 */
public class IOManager {

	public static final String MAPS_FOLDER = "Maps//";
	public static final String MAPS_LIST_FILENAME = "ListaDeMapas.txt";
	public static final String RANKING_FILENAME = "Ranking.txt";

	private LinkedList<Map> mapsList = new LinkedList<Map>();

	public IOManager() {
		try {
			File mapsListFile = new File(MAPS_LIST_FILENAME);
			if(!mapsListFile.exists())
				mapsListFile.createNewFile();
			
			File rankingFile = new File(RANKING_FILENAME);
			if(!rankingFile.exists())
				rankingFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public LinkedList<Map> getMapsList() {
		return mapsList;
	}


	public void loadMapFiles() {
		LinkedList<String> filenamesList = loadMainFile();

		for (int i = 0; i < filenamesList.size(); i++) {
			loadMap(filenamesList.get(i), (i + 1));
		}
	}


	private void loadMap(final String filename, final int id) {
		Map map = new Map(id);
		int x = 0, y = 0;

		try {
			Scanner reader = new Scanner(new File(MAPS_FOLDER + filename));
			try{
				while(reader.hasNextLine()){

					Scanner rowInterpreter = new Scanner(reader.nextLine());
					try{
						while(rowInterpreter.hasNext()){
							String word = rowInterpreter.next();
							if(GameMath.isNumber(word)){
								int pathIndex = Integer.parseInt(word);
								Path path = new Path(new Coordinate(x, y), pathIndex);
								map.add(path);

							} else if(word.charAt(0) == '*'){
								Wall wall = new Wall(new Coordinate(x, y));
								map.add(wall);

							} else if(word.charAt(0) == '+'){
								Grass grass = new Grass(new Coordinate(x, y));
								map.add(grass);
							}

							x++;
						}

						x = 0;
						y++;

					} finally {
						rowInterpreter.close();
					}
				}

			} finally{
				reader.close();
			}

			mapsList.add(map);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	private LinkedList<String> loadMainFile(){
		LinkedList<String> filenamesList = new LinkedList<String>();

		try {
			Scanner reader = new Scanner(new File(MAPS_FOLDER + MAPS_LIST_FILENAME));
			try{
				while(reader.hasNextLine()){
					filenamesList.add(reader.nextLine().trim());
				}
			} finally{
				reader.close();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return filenamesList;
	}


	public static LinkedList<RankingEntry> loadAndSaveRanking(final RankingEntry rankingEntry){
		LinkedList<RankingEntry> rankingEntries = new LinkedList<RankingEntry>();

		try {
			Scanner reader = new Scanner(new File(RANKING_FILENAME));
			try{ 
				while(reader.hasNextLine()){
					rankingEntries.add(new RankingEntry(reader));
				}
			} finally {
				reader.close();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}


		rankingEntries.add(rankingEntry);
		Collections.sort(rankingEntries, new RankingEntryComparator());


		try {
			PrintWriter writer = new PrintWriter(new File(RANKING_FILENAME));
			try{
				for (int i = 0; i < 2; i++) {
					rankingEntries.get(i).writeEntry(writer);
				}
			} finally {
				writer.close();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return rankingEntries;
	}



}